<?php 
/***
 * 授权页面
 */
// include our OAuth2 Server object
require_once __DIR__.'/server.php';
 
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
 
// validate the authorize request
if (!$server->validateAuthorizeRequest($request, $response)) {
    $response->send();
    die;
}
// display an authorization form
if (empty($_POST)) {
  exit('
<form method="post">
  <label>授权TestClient?</label><br />
  <p><span>账号:</span><input type="text" name="username" value=""></p>
  <p><span>密码:</span><input type="password" name="password" value="" /></p>
  <input type="submit" name="authorized" value="yes">
  <input type="submit" name="authorized" value="no">
</form>');
}
 
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');

//todo::账号密码 对应的需要切换到平台的账号 去做验证 这里固定值
$user_id = '1234';
$server->handleAuthorizeRequest($request, $response, $is_authorized,$user_id);
if ($is_authorized) {
  // this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
  $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);

  $response->setRedirect(302,$_GET['redirect_uri'].'?code='.$code."&state=".$_GET['state']);
  //exit("SUCCESS! Authorization Code: $code");
}
$response->send();